$(function() {
	//初始化datatable
	$("#table").XRDataTable({
		ajaxUrl: "Admin/Attend/getDatas",
		paramFunc: paramFunc,
		operation: {
			"icon-list": ["Admin/Attend/edit/#id",'督导听课评价表',false,900],
			"edit": ["Admin/Class_Assess/attendAdd/#id"],
			"icon-print": [printAttend,'打印'],
			"icon-reply": [replyAttend,'退回'],
			"icon-trash": [deleteAttend,'删除'],
		},
		hasCheckbox: false,
		afterFunc:afterFunc
	});

	laydate.render({
	    elem: '#start_time', //指定元素
	    trigger: 'click'
	});
	laydate.render({
	    elem: '#end_time', //指定元素
	    trigger: 'click'
	});

	// exportEvent();

	$(".resetButton").click(function(event) {
		$("input[name='listen_name']").val('');
		$('.searchButton').click();
	});
});

var paramFunc = function() {
	var data = getFormJson();
	return data;
};

var afterFunc = function(){

    $("#table tbody tr").each(function() {
        var data = $("#table").getOpts().datas[$(this).index()];
        if(data.admin_type==1){
        	$(this).children("td:nth-child(2)").find(".icon-lock").hide();
        }
        if(data.is_show == 1){
        	$(this).children("td:nth-child(2)").find(".icon-reply").show();
        }else{
        	$(this).children("td:nth-child(2)").find(".icon-reply").hide();
        }
    });
}

var printAttend = function(idx, trData){

	//window.location.href="Admin/Attend/edit/"+trData.id+"?";
	window.open("Admin/Attend/edit/"+trData.id+"?");
}


var replyAttend = function(idx, trData){
	var msg = "是否确认退回听课【" + trData.listen_name + "】的听课记录?";
	swal({
		title: "请确认",
		text: msg,
		type: "warning",
		showCancelButton: true,
		closeOnConfirm: true,
		confirmButtonText: "确定",
		cancelButtonText: '取消',
		confirmButtonColor: "#ec6c62"
	}, function(isConfirm) {
		if(isConfirm) {
			$.ajax({
				type: "post",
				url: "Admin/Attend/reply/"+trData.id,
				async: true,
				success: function(data) {
					var ret = $.parseJSON(data);
					if(ret.status) {
						window.parent.topMessagePrompt('success',ret.msg);
	                    window.parent.refreshTable();
					} else {
						window.parent.topMessagePrompt('error',ret.msg);
					}
				}
			});
		}
	});
}


var deleteAttend = function(idx, trData) {
	var msg = "是否确认删除该听课?";
	swal({
		title: "请确认",
		text: msg,
		type: "warning",
		showCancelButton: true,
		closeOnConfirm: true,
		confirmButtonText: "确定",
		cancelButtonText: '取消',
		confirmButtonColor: "#ec6c62"
	}, function(isConfirm) {
		if(isConfirm) {
			$.ajax({
				type: "post",
				url: "Admin/Attend/delete/"+trData.id,
				async: true,
				success: function(data) {
					var ret = $.parseJSON(data);
					if(ret.status) {
						window.parent.topMessagePrompt('success',ret.msg);
	                    window.parent.refreshTable();
					} else {
						window.parent.topMessagePrompt('error',ret.msg);
					}
				}
			});
		}
	});
};

//导出数据
exportEvent = function() {
	$("input").keypress(function(event) {
		if(event.keyCode == "13") {
			$("#table").search();
		}
	})
	$("#export").click(function() {
		var ret = $("#table").getTableThInfo();
		ret.params = usefulArray(paramFunc());
		ret.title = "会员列表";
		locationPost("User/commonExport", {
			'json': JSON.stringify(ret)
		});
	});
};


function Export(){		
	var data = getFormJson();
	var msg = "请确认是否导出?";
    swal({
        title: "请确认",
        text: msg,
        type: "warning",
        showCancelButton: true,
        closeOnConfirm: true,
        confirmButtonText: "确定",
        cancelButtonText: '取消',
        confirmButtonColor: "#ec6c62"
    }, function(isConfirm) {
        if(isConfirm) {
            var load_alert=loadAlert('数据导出中，请稍后...');
            $.ajax({
                type: "post",
                url: "Admin/Attend/getDatas/1/true",
                data:data,
                async: true,
                dataType:'json',
                success: function(data) {
                    closeAlert(load_alert);
                    if(data.status) {
                        location.href=getRootPath()+'/'+data.v;
                    } else {
                        window.parent.topMessagePrompt('error',ret.msg);
                    }
                }
            });
        }
    });
}


